package com.sh.shanhuo.activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sh.shanhuo.R;
import com.sh.shanhuo.adapter.RecommendationAdapter;
import com.sh.shanhuo.bean.PushListBean;
import com.sh.shanhuo.listener.GsonObjectCallback;
import com.sh.shanhuo.utils.OkHttpUtil;
import com.sh.shanhuo.utils.ScreenSwitch;
import com.sh.shanhuo.utils.Tools;
import com.sh.shanhuo.view.CommonItemDecoration;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.Call;

public class RecommendationListActivity extends BaseActivity {
    private RecommendationAdapter adapter;
    private SmartRefreshLayout smartLayout;
    private List<PushListBean.RowsBean> rowsBeans;
    private int pageNum;
    private String userName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recommendation_list;
    }

    @Override
    protected void initView() {
        super.initView();
        getBundle();
        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> finish());
        smartLayout = findViewById(R.id.smartLayout);
        smartLayout.setEnableLoadMore(true);
        smartLayout.setEnableRefresh(true);
        smartLayout.setOnLoadMoreListener(refreshLayout -> getPushList());
        smartLayout.setOnRefreshListener(refreshLayout -> getPushList());
        initRecyclerView();
    }

    private void getBundle() {
        Intent intent = getIntent();
        if (intent == null)
            return;
        Bundle bundle = intent.getExtras();
        if (bundle == null)
            return;
        bundle.getInt("position", 0);
        userName = bundle.getString("userName", "");
    }

    private void initRecyclerView() {
        RecyclerView recommendationView = findViewById(R.id.recommendationView);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recommendationView.setLayoutManager(manager);
        recommendationView.addItemDecoration(new CommonItemDecoration(Tools.dp2px(mContext, 10),
                Tools.dp2px(mContext, 10), Tools.dp2px(mContext, 10)));
        adapter = new RecommendationAdapter(this, null);
        recommendationView.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putInt("siteId", rowsBeans.get(position).getId());
            bundle.putBoolean("hasList", true);
            bundle.putString("userName", userName);
            bundle.putString("pushId", rowsBeans.get(position).getId() + "");
            ScreenSwitch.startActivity(RecommendationListActivity.this,
                    RecommendationInfoActivity.class, bundle);
        });
        getPushList();
    }

    private void getPushList() {
        onShowLoading();
        Map<String, String> params = new HashMap<>();
        params.put("pageSize", "20");
        params.put("pageNum", pageNum + "");
        params.put("orderByColumn", "");
        params.put("isAsc", "");

        Map<String, String> header = new HashMap<>();
        if (!TextUtils.isEmpty(userName)) {
            header.put("userName", userName);
        } else {
            header.put("uuid", Tools.getUniquePsuedoID());
        }

        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.PUSH_LIST, header, params, new GsonObjectCallback<PushListBean>() {
            @Override
            public void onUi(PushListBean pushListBean) {
                smartLayout.finishLoadMore();
                smartLayout.finishRefresh();
                if (pushListBean.getCode() == 200 && "查询成功".equals(pushListBean.getMsg())) {
                    rowsBeans = pushListBean.getRows();
                    Collections.sort(rowsBeans, (o1, o2) -> (int) (Tools.timeSample(o2.getUpdateTime()) -
                            Tools.timeSample(o1.getUpdateTime())));
                    adapter.setNewData(rowsBeans);
                    pageNum += pushListBean.getTotal();
                }
                onDismissLoading();
            }

            @Override
            public void onFailed(Call call, IOException e) {
                onDismissLoading();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        smartLayout.finishLoadMore();
    }
}
